package py.com.erpone.session.dm;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import org.primefaces.model.DualListModel;

import py.com.erpone.entity.dm.Pais;
import py.com.erpone.entity.dm.Presentacion;
import py.com.erpone.session.dm.list.PresentacionList;


@Stateful
@Named
@LocalBean
@ConversationScoped
public class PresentacionBean {

	@PersistenceContext(type=PersistenceContextType.EXTENDED)
	private EntityManager em;
	
	@Inject
	private Conversation conversation;
	
	private Presentacion presentacion;
	
	@Inject
	private PresentacionList presentacionList;
	
	private DualListModel<Presentacion> presentaciones;
	
	public PresentacionBean(){
	
        
	}


	public Presentacion getPresentacion() {
		return presentacion;
	}


	public void setPais(Presentacion presentacion) {
		this.presentacion = presentacion;
	}
	
	
	public DualListModel<Presentacion> getPresentaciones() {
		return presentaciones;
	}


	public void setPresentaciones(DualListModel<Presentacion> presentaciones) {
		this.presentaciones = presentaciones;
	}


	public void iniciar(){
		if(conversation.isTransient())
			conversation.begin();
		presentacion=new Presentacion();
	}
	
	public void iniciar(Presentacion presentacion){
		if(conversation.isTransient())
			conversation.begin();
		this.presentacion=em.find(Presentacion.class, presentacion.getId());
		

	}
	
	
	public void guardar(){
		if(presentacion.getId()==null)
			em.persist(presentacion);
		else
			em.merge(presentacion);
		if(!conversation.isTransient())
			conversation.end();
	
	}
	
	public void eliminar(Presentacion presentacion){
		em.remove(em.merge(presentacion));
		if(!conversation.isTransient())
			conversation.end();
		
	}
	
	public String cancelar(){
		if(!conversation.isTransient())
			conversation.end();
		return "/dm/presentaciones.xhtml";
	}
	
}
